<template>
  <div class="discovery-container">
    <!-- 轮播图 -->
    <el-carousel class="" :interval="4000" type="card">
      <el-carousel-item 
      v-for="(item, ind) in banners" 
      :key="ind"
      >
        <img :src="item.imageUrl" alt="" />
      </el-carousel-item>
    </el-carousel>
    <!-- 推荐歌单 -->
    <div class="recommend">
      <h3 class="title">
        推荐歌单
      </h3>
      <div class="items">
        <div class="item"
        v-for="item in Lists"
        :key="item.id"
        @click="$router.push(`/playlist?id=${item.id}`)"
        >
          <div class="img-wrap">
            <div class="desc-wrap">
              <span class="desc">{{item.copywriter}}</span>
            </div>
            <img :src="item.picUrl" alt="" />
            <span class="iconfont icon-play"></span>
          </div>
          <p class="name">{{item.name}}</p>
        </div>
      </div>
    </div>
    <!-- 最新音乐 -->
    <div class="news">
      <h3 class="title">
        最新音乐
      </h3>
      <div class="items">
        <div class="item"
        v-for="item in songs"
        :key="item.id"
        @click="PlayMusic(item.id)"
        >
          <div class="img-wrap">
            <img :src="item.picUrl" alt="" />
            <span  class="iconfont icon-play"></span>
          </div>
          <div class="song-wrap">
            <div class="song-name">{{item.name}}</div>
            <div class="singer">{{item.song.artists[0].name}}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 推荐MV -->
    <div class="mvs">
      <h3 class="title">推荐MV</h3>
      <div class="items">
        <div class="item"
        v-for="item in mvs"
        :key="item.id"
        >
          <div class="img-wrap">
            <img :src="item.picUrl" alt="" />
            <span class="iconfont icon-play" @click="PlayMusic(item.id)"></span>
            <div class="num-wrap">
              <div class="iconfont icon-play"></div>
              <div class="num">{{item.playCount}}</div>
            </div>
          </div>
          <div class="info-wrap">
            <div class="name">{{item.name}}</div>
            <div class="singer">{{item.artistName}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {getBanner, personalized, newsong, mv, playMusic} from '../network/request'
  export default {
    name: 'discovery',
    data() {
      return {
        banners: [],
        Lists: [],
        songs: [],
        mvs: []
      }
    },
    async created() {
      const res = await getBanner();
      this.banners = res.banners;

      const res2 = await personalized(10);
      this.Lists = res2.result;

      const res3 = await newsong();
      this.songs = res3.result;

      const res4 = await mv();
      this.mvs = res4.result;
    },
    methods: {
      async PlayMusic(id) {
        const res = await playMusic(id)
        let mp3Url = res.data[0].url;
        if(!mp3Url) {
          return this.$message.error('该资源为VIP专享，暂不支持播放 ！')
        }
        //改变父组件绑定的播放音频
        this.$parent.musicUrl = mp3Url;
      },
      // async playMusic(id) {
      //   const res = await playMusic(id)
      //   if (!res.data[0].url) {
      //     return this.$message.error('该资源为VIP专享，暂不支持播放 ！')
      //   }
      //   // 设置给父组件的播放地址
      //   this.$parent.musicUrl = res.data[0].url
      // }
    },
  };
</script>

<style >

</style>
